package com.example.demo.controller;

import com.example.demo.pojo.Department;
import com.example.demo.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @RequestMapping("/deps")
    public String list(Model model){
        model.addAttribute("deps", departmentService.getAllTheDepartments());
        return "dep/dep_list";
    }

    @GetMapping("/adddep")
    public String toAdddep() {
        return "dep/add";
    }

    @PostMapping("/dep")
    public String addEmp(Department department) {
        System.out.println("添加部门"+department);
        departmentService.addNewDepartment(department);
        return "redirect:/deps";
    }

    @GetMapping("/dep/{id}")
    public String toUpdateDep(@PathVariable("id")Integer id, Model model){
        model.addAttribute("dep",departmentService.getDepartmentById(id));
        return "dep/update";
    }

    @PostMapping("/updatedep")
    public String updateDep(Department department){
        System.out.println("更新"+department);
        departmentService.updateDepartment(department);
        return "redirect:/deps";
    }

    @GetMapping("/deldep/{id}")
    public String deleteEmp(@PathVariable("id")int id){
        System.out.println("删除"+id+"部门");
        departmentService.deleteDepartmentById(id);
        return "redirect:/deps";
    }
}
